package mobidever.mygodutch.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import mobidever.mygodutch.R;
import mobidever.mygodutch.bussiness.BussinessCategory;
import mobidever.mygodutch.model.ModelCategory;
import mobidever.mygodutch.model.ModelUser;

public class AdapterCategory extends BaseExpandableListAdapter
{

	private BussinessCategory mBussinessCategory;
	private Context mContext;
	private List mRootList;
	private List mChildCountOfGroup;

	private class GroupHolder
	{
		TextView tvName;
		TextView tvCount;

	}

	private class Childholer
	{
		TextView tvName;
	}

	public AdapterCategory(Context pContext)
	{
		super();
		mContext = pContext;
		mBussinessCategory = new BussinessCategory(mContext);
		mRootList = mBussinessCategory.getNoHideRootCategory();
		mChildCountOfGroup = new ArrayList();
	}

	public Object getChild(int groupPosition, int childPosition)
	{
		ModelCategory _ModelCategory = (ModelCategory) getGroup(groupPosition);
		List _List = mBussinessCategory
				.getNoHideCategoryListByParentID((int) _ModelCategory
						.getCategoryID());
		return _List.get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition)
	{
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent)
	{
		Childholer _Childholer;

		if (convertView == null)
		{
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.category_children_list_item, null);
			_Childholer = new Childholer();

			_Childholer.tvName = (TextView) convertView
					.findViewById(R.id.tvCategoryChildrenName);

			convertView.setTag(_Childholer);
		} else
		{
			_Childholer = (Childholer) convertView.getTag();
		}

		ModelCategory _ModelCategory = (ModelCategory) getChild(groupPosition, childPosition);
		_Childholer.tvName.setText(_ModelCategory.getCategoryName());
		return convertView;
	}

	public int getChildrenCount(int groupPosition)
	{		
		ModelCategory _ModelCategory = (ModelCategory) mRootList.get(groupPosition);
		List _List = mBussinessCategory.getNoHideCategoryListByParentID((int)_ModelCategory.getCategoryID());
		
		return _List.size();
	}

	public Object getGroup(int groupPosition)
	{

		return mRootList.get(groupPosition);
	}

	public int getGroupCount()
	{
		return mRootList.size();
	}

	public long getGroupId(int groupPosition)
	{
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent)
	{
		GroupHolder _GroupHolder;
		if (convertView == null)
		{
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.category_group, null);
			_GroupHolder = new GroupHolder();

			_GroupHolder.tvName = (TextView) convertView
					.findViewById(R.id.tvCategoryName);
			_GroupHolder.tvCount = (TextView) convertView
					.findViewById(R.id.tvCount);

			convertView.setTag(_GroupHolder);
		} else
		{
			_GroupHolder = (GroupHolder) convertView.getTag();
		}

		ModelCategory _ModelCategory = (ModelCategory) getGroup(groupPosition);
		_GroupHolder.tvName.setText(_ModelCategory.getCategoryName());
		int _Count = mBussinessCategory.getNoHideCountByParentID((int) _ModelCategory.getCategoryID());
		_GroupHolder.tvCount.setText(mContext.getString(R.string.TextViewTextChildrenCategory, _Count));
		mChildCountOfGroup.add(_Count);
		return convertView;
	}

	public int getChildCountOfGroup(int pGroupPosition)
	{
		return (Integer) mChildCountOfGroup.get(pGroupPosition);
	}
	
	public boolean hasStableIds()
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition)
	{
		return true;
	}
}
